
<template>
    <div class="iframe-cont">
        <div class="content">
            <div class="title">{{$route.meta.title}} </div>
            <el-divider></el-divider>
            <!--     筛选区       -->
            <div class="f f-a-c">
                <el-input
                    class="search-group mar-left-20"
                    style="width: 280px"
                    placeholder="输入二维码编号或设备编号"
                    size="small"
                    clearable
                    v-model="queryParams.keyword"
                    @keydown.native.enter="handleQuery"
                >
                    <el-button slot="append" icon="el-icon-search" @click="handleQuery"></el-button>
                </el-input>
                <div class="f-1"></div>
                <el-button style="margin-left: 20px" type="primary" size="small" icon="el-icon-plus" @click="addCodeDialog=true">批量生成二维码</el-button>
            </div>

            <el-table class="table th-bg"
                      :data="dataList"
                      v-loading="loading"
                      element-loading-text="加载中"
                      element-loading-spinner="el-icon-loading"
            >
                <el-table-column label="序号" prop="name" width="60" fixed>
                    <template slot-scope="scope">
                        <span>{{(queryParams.page - 1) * queryParams.limit + scope.$index + 1}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="批次" prop="batchNo"></el-table-column>
                <el-table-column label="总数量" prop="totalNum"></el-table-column>
                <el-table-column label="可用数量" prop="canUseNum"></el-table-column>
                <el-table-column label="生成日期" prop="addTime"></el-table-column>
                <el-table-column label="操作" width="150px" align="center" fixed="right">
                    <template slot-scope="{row}">
                        <el-button type="text" @click="seeBatchCode(row)">查看</el-button>
                        <el-button type="text" @click="downloadCode(row)">下载</el-button>
                        <el-button type="text" @click="delCodes(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.page"
                :limit.sync="queryParams.limit"
                @pagination="getList"
            />
        </div>

<!--        生成二维码-->
        <el-dialog
            :append-to-body="true"
            title="批量生成二维码"
            :visible.sync="addCodeDialog"
            width="440px"
            center
        >
            <el-input class="form-comp-item" type="number" v-model="codeNum" placeholder="请输入二维码数量"></el-input>
            <div slot="footer">
                <el-button @click="addCodeDialog=false">取 消</el-button>
                <span style="display: inline-block;width: 20px;"></span>
                <el-button type="primary" @click="genCode">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import SearchMacType from "@/components/SearchCondition/SearchMacType";
import {Message} from "element-ui";
import {enableOrDisableDevice, reAllocateDevice} from "@/api/device";
import {previewImg} from "@/utils/previewImg";
import {genQrcode} from "@/api/common";
import ParamsSet from "@/components/ParamsSet";
import SearchDeviceClass from "@/components/SearchCondition/SearchDeviceClass";
import {downLoadZip, resolveBlob} from "@/utils";
import Vue from "vue";
export default {
    name: "index",
    components: {SearchDeviceClass, ParamsSet, SearchMacType},
    data(){
        return{
            dataList: [1],
            loading:false,//加载中
            addCodeDialog: false,
            total:10,
            queryParams: {
                limit: this.$tmp.PAGE_SIZES[0],
                page: 1,
                keyword: null,
            },
            codeNum: null, // 二维码数量
        }
    },
    created() {
        this.getList();
    },
    methods:{

        handleQuery() {
            this.queryParams.page = 1
            this.getList()
        },
        downloadCode(row) {
            let url = this.$baseurl + '/api/qrcode/batch/download?id=' + row.id
            window.open(url)
        },
        seeBatchCode(row) {
            this.$router.push({ path: '/device/codeManage/codeList', query: { id: row.id, batchNo: row.batchNo } })
        },
        delCodes(row){
            this.$alert(`确认删除当前批次的全部二维码吗？`, '提示').then(() => {
                this.$http({
                    url:'/api/qrcode/batch/delete',
                    data:{
                        id: row.id
                    }
                }).then(res=>{
                    Message.success('删除成功！')
                    this.getList()
                }).catch(() => {
                    this.$confirm(`当前批次中存在已使用的二维码，删除失败`, '提示', {
                        confirmButtonText: '关 闭',
                        showCancelButton: false,
                        type: 'warning',
                        center: true,
                    }).then(() => {

                    })
                })
            })
        },
        genCode() {
            this.$http({
                url:'/api/qrcode/batch/add',
                method: 'post',
                data: { totalNum: this.codeNum}
            }).then(res=>{
                Message.success('批量生成成功！')
                this.addCodeDialog = false
                this.getList()
            }).catch(() => {
            })

        },
        //获取数据
        getOperators(){
            this.$http({
                url:'/api/operator/page',
                method: 'get',
                data: this.operatorQuery
            }).then(res=>{
                this.operators = res.data;
                this.operatorTotal = res.count;
            });
        },
        readyParamsSet(row) {
            this.macId = row.id
            this.paramDialog = true
        },
        gotoDeviceDetail(row) {
            this.$router.push('/device/soldOut/detail?id=' + row.id || '')
        },
        addDevice(row={}) {
            this.$router.push({ path: '/device/soldOut/edit?id=' ,query:{ id:  row.id || ''}})
        },
        handleMacType(e) {
            this.queryParams.deviceClass = e.ids
            this.handleQuery()
        },
        //获取数据
        getList() {
            this.loading = true;
            this.$http({
                url:'/api/qrcode/batch/page',
                method: 'get',
                data: this.queryParams,
            }).then(res=>{
                this.dataList = res.data;
                this.total = res.count;
            }).finally(() => {
                this.loading = false
            })
        },

    }
}
</script>

<style lang="scss" scoped>
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
    -webkit-appearance: none;
}
::v-deep input[type="number"]{
    -moz-appearance: textfield;
}
::v-deep .el-form-item__content {
    margin-left: 10px;
}
.form-item {
    width: 400px;
}

.table {
    margin-top: 20px;
}
::v-deep .el-table__row {
    cursor: pointer;
}
</style>
